package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShopCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("userShopCart")
@RequestMapping("/user/shoppingCart")
@Api(tags = "用户端-购物车")
@Slf4j
@RequiredArgsConstructor
public class ShoppingCartController {
    private final ShopCartService shopCartService;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     * @return
     */
    @ApiOperation(value = "添加购物车")
    @PostMapping("/add")
    public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("用户端-添加购物车: {}", shoppingCartDTO);
        //调用业务接口添加购物车
        shopCartService.addCart(shoppingCartDTO);
        return Result.success();
    }

    /**
     * 查询购物车
     *
     * @return
     */
    @ApiOperation(value = "查询购物车")
    @GetMapping("/list")
    public Result<List<ShoppingCart>> list() {
        List<ShoppingCart> list = shopCartService.list();
        return Result.success(list);
    }

    @ApiOperation("清空购物车")
    @DeleteMapping("clean")
    public Result clean() {
        shopCartService.clean();
        return Result.success();
    }

    /**
     * 购物车商品数量修改
     *
     * @return
     */
    @ApiOperation("购物车商品数量修改")
    @PostMapping("/sub")
    public Result sub(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("用户端-购物车商品数量修改: {}", shoppingCartDTO);
        shopCartService.subCart(shoppingCartDTO);
        return Result.success();
    }
}
